<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  <title>PHP扩展功能--发送邮件 | 程序小工  | Just do it!</title>
  <meta name="description" content="遇见更好的自己~" />
  <meta name="keywords" content="" />
  <meta name="HandheldFriendly" content="True" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <link rel="shortcut icon" href="/">
  <link rel="alternate" href="/atom.xml" title="程序小工  | Just do it!">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1、下载 PHPMailer 源码 [github 下载] （测试使用的是 5.2.2 版本）  2、注册并登录网易邮箱 （其他邮箱均可）【用于配置用户名和三方登录授权码，以及发送人邮箱地址】 （1）开启 POP3 协议 定位到开启页面">
<meta name="keywords" content="email">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP扩展功能--发送邮件">
<meta property="og:url" content="http://blog.i2arch.tech/2018/04/16/php-email/index.html">
<meta property="og:site_name" content="程序小工  | Just do it!">
<meta property="og:description" content="1、下载 PHPMailer 源码 [github 下载] （测试使用的是 5.2.2 版本）  2、注册并登录网易邮箱 （其他邮箱均可）【用于配置用户名和三方登录授权码，以及发送人邮箱地址】 （1）开启 POP3 协议 定位到开启页面">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327171400339-1436550372.png">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327171327537-1436075640.png">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327163714902-341662125.png">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327163714902-341662125.png">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327170739982-368374531.png">
<meta property="og:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327170830831-1433469872.png">
<meta property="og:updated_time" content="2018-08-14T07:59:24.562Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PHP扩展功能--发送邮件">
<meta name="twitter:description" content="1、下载 PHPMailer 源码 [github 下载] （测试使用的是 5.2.2 版本）  2、注册并登录网易邮箱 （其他邮箱均可）【用于配置用户名和三方登录授权码，以及发送人邮箱地址】 （1）开启 POP3 协议 定位到开启页面">
<meta name="twitter:image" content="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327171400339-1436550372.png">
    
  <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
  <link href='//cdn.bootcss.com/node-waves/0.7.5/waves.min.css' rel='stylesheet'>
  <link rel="stylesheet" href="/style.css">
  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>
</head>

<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script>
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				程序小工  | Just do it!
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-home' href='/'>
								<i class='icon icon-list2'></i>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								<i class='icon icon-bookmark'></i>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								<i class='icon icon-user'></i>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <!-- 左侧内容 -->
      <div class='l_main'>
        <article id="post-php-email"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>

	<h2 class="title">
  	<a href="/2018/04/16/php-email/">
    	PHP扩展功能--发送邮件
    </a>
	</h2>

	<div class="pagenav">
		<time>
			2018-04-16
		</time>

		
    
    <div class='cats'>
        <i class="icon icon-bookmark"></i>
        <a href="/categories/PHP/">PHP</a>
    </div>


		
			<div class="pagetags">
				<i class="icon icon-price-tag" style="margin-right: 2px;"></i>
				
					<a href="/tags/email/" style="margin-left: 3px;">
						email
					</a>
				
			</div>
			
	</div>
	</section>

	
	<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-下载-phpmailer-源码"><span class="toc-text"> 1、下载 PHPMailer 源码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-注册并登录网易邮箱"><span class="toc-text"> 2、注册并登录网易邮箱</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-自定义封装邮件类"><span class="toc-text"> 3、自定义封装邮件类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-发送邮件调用邮件发送类"><span class="toc-text"> 4、发送邮件，调用邮件发送类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-浏览器测试"><span class="toc-text"> 5、浏览器测试</span></a></li></ol></section>
	

	<section class="article typo">
		<div class="article-entry" itemprop="articleBody">
				<span class="zhaiyao">[摘要]</span>
			<h3 id="1-下载-phpmailer-源码"><a class="markdownIt-Anchor" href="#1-下载-phpmailer-源码"></a> 1、下载 PHPMailer 源码</h3>
<p>[github 下载]<br>
（测试使用的是 5.2.2 版本）</p>
<h3 id="2-注册并登录网易邮箱"><a class="markdownIt-Anchor" href="#2-注册并登录网易邮箱"></a> 2、注册并登录网易邮箱</h3>
<p>（其他邮箱均可）【用于配置用户名和三方登录授权码，以及发送人邮箱地址】</p>
<p>（1）开启 POP3 协议 定位到开启页面</p>
<a id="more"></a>
<p><img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327171400339-1436550372.png" alt=""></p>
<p>（2）开启三方登录授权，并获取授权码（一串字符串）<br>
<img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327171327537-1436075640.png" alt=""></p>
<h3 id="3-自定义封装邮件类"><a class="markdownIt-Anchor" href="#3-自定义封装邮件类"></a> 3、自定义封装邮件类</h3>
<p>（1）核心文件（进行重命名）：</p>
<p><img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327163714902-341662125.png" alt=""></p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">class.phpmailer.php   ====》  PHPMailer.class.php</span><br><span class="line"></span><br><span class="line">class.pop3.php  ====》POP3.class.php</span><br><span class="line"></span><br><span class="line">class.smtp.php  ====》SMTP.class.php</span><br></pre></td></tr></table></figure>
<p>并拷贝到 PHPMailer 目录下<br>
<img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327163714902-341662125.png" alt=""><br>
（2）邮件发送类封装：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">require_once</span> <span class="string">'PHPMailer/PHPMailer.class.php'</span>;</span><br><span class="line"><span class="keyword">require_once</span> <span class="string">'PHPMailer/SMTP.class.php'</span>;</span><br><span class="line"><span class="keyword">require_once</span> <span class="string">'PHPMailer/POP3.class.php'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Email</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">sendMail</span><span class="params">($title,$content,$to)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $mail = <span class="keyword">new</span> PHPMailer();</span><br><span class="line">        $mail -&gt; IsSMTP();                       <span class="comment">//告诉服务器使用smtp协议发送</span></span><br><span class="line">        $mail -&gt; SMTPAuth = <span class="keyword">true</span>;                <span class="comment">//开启SMTP授权</span></span><br><span class="line">        $mail -&gt; Host = <span class="string">'smtp.163.com'</span>;          <span class="comment">//告诉我们的服务器使用163的smtp服务器发送</span></span><br><span class="line">        $mail -&gt; From = <span class="string">'Muse_girlo@163.com'</span>;    <span class="comment">//发送者的邮件地址</span></span><br><span class="line">        $mail -&gt; FromName = <span class="string">'Muse_girlo'</span>;        <span class="comment">//发送邮件的用户昵称</span></span><br><span class="line">        $mail -&gt; Username = <span class="string">'Muse_girlo'</span>;        <span class="comment">//登录到邮箱的用户名</span></span><br><span class="line">        $mail -&gt; Password = <span class="string">'xxxxxxxxxx'</span>;        <span class="comment">//第三方登录的授权码，在邮箱里面设置</span></span><br><span class="line">        <span class="comment">//编辑发送的邮件内容</span></span><br><span class="line">        $mail -&gt; IsHTML(<span class="keyword">true</span>);            <span class="comment">//发送的内容使用html编写</span></span><br><span class="line">        $mail -&gt; CharSet = <span class="string">'utf-8'</span>;                <span class="comment">//设置发送内容的编码</span></span><br><span class="line">        $mail -&gt; Subject = $title;                <span class="comment">//设置邮件的主题、标题</span></span><br><span class="line">        $mail -&gt; MsgHTML($content);                <span class="comment">//发送的邮件内容主体</span></span><br><span class="line">        <span class="comment">//告诉服务器接收人的邮件地址</span></span><br><span class="line">        $mail -&gt; AddAddress($to);</span><br><span class="line">        <span class="comment">//调用send方法，执行发送</span></span><br><span class="line">        $result = $mail -&gt; Send();</span><br><span class="line">        <span class="keyword">if</span>($result)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $mail -&gt; ErrorInfo;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-发送邮件调用邮件发送类"><a class="markdownIt-Anchor" href="#4-发送邮件调用邮件发送类"></a> 4、发送邮件，调用邮件发送类</h3>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">$title = <span class="string">"测试邮件发送功能"</span>;</span><br><span class="line">$content = <span class="string">"脚踏实地，坚持学习！"</span>;</span><br><span class="line">$to = <span class="string">"zqunor@foxmail.com"</span>;</span><br><span class="line"></span><br><span class="line">$res = Email::sendMail($title, $content, $to);</span><br><span class="line"><span class="keyword">if</span> ($res) &#123;</span><br><span class="line">    <span class="keyword">echo</span> <span class="string">'邮件发送成功！'</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">echo</span> <span class="string">"邮件发送失败！"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="5-浏览器测试"><a class="markdownIt-Anchor" href="#5-浏览器测试"></a> 5、浏览器测试</h3>
<p><img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327170739982-368374531.png" alt=""></p>
<p><img src="https://images2018.cnblogs.com/blog/1049028/201803/1049028-20180327170830831-1433469872.png" alt=""></p>

		</div>

		
			<div class="art-item-footer">
					
						<span class="art-item-left"><i class="icon icon-chevron-thin-left"></i> <a href="/2018/04/16/tool-git/" rel="prev" title="使用Git和Github进行代码管理">
							使用Git和Github进行代码管理
						</a>上一篇文章</span>
					
					
						<span class="art-item-right">下一篇文章：<a href="/2018/04/16/php-curl/" rel="next"  title="PHP扩展功能--cURL">
							PHP扩展功能--cURL
						</a><i class="icon icon-chevron-thin-right"></i></span>
					
			</div>
		

		
			

    <div class="recommended_posts">
        <h3>推荐文章</h3>
        <ul>
            
                <li><a href="http://blog.i2arch.tech/2018/04/26/composer-image/">Composer设置国内镜像</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/04/16/tool-git-remote-add/">Git项目管理--将现有项目提交到空仓库</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/04/16/tool-git/">使用Git和Github进行代码管理</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/04/16/php-curl/">PHP扩展功能--cURL</a></li>
            
        </ul>
    </div>


		
	</section>

	

</article>
<script>
	window.subData = {
		title: 'PHP扩展功能--发送邮件',
		tools: true
	}
</script>

      </div>

      <!-- 右侧边栏组件 -->
      <aside class='l_side'>
        
      </aside>

      <div class="gotop">
          
    <a href='javascript:void(0)'>
        <i class="icon icon-arrow_upward flat-box">
        </i>
    </a>

      </div>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/zqunor" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://www.cnblogs.com/zqunor" class="social zhihu"
          target="_blank" rel="external">
          <span class="icon icon-zhihu"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a>  Powered by<a href="https://blog.i2arch.tech/" target="_blank">程序小工</a>.</div>

</footer>

  <script>setLoadingBarProgress(80);</script>
  

    <script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
    <script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
    <script src="/js/jquery.fitvids.js"></script>
      <script>
        var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
        var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
        var ALGOLIA_API_KEY = "";
        var ALGOLIA_APP_ID = "";
        var ALGOLIA_INDEX_NAME = "";
        var AZURE_SERVICE_NAME = "";
        var AZURE_INDEX_NAME = "";
        var AZURE_QUERY_KEY = "";
        var BAIDU_API_ID = "fca5b83cf69f752ddc96a61ef64a3659";
        var SEARCH_SERVICE = "hexo";
        var ROOT = "/" || "/";
        if (!ROOT.endsWith('/')) ROOT += '/';
      </script>
      <script src="/js/search.js"></script>
        <script src="/js/app.js"></script>
  <script>setLoadingBarProgress(100);</script>
</body>
</html>
